package com.example.demo.email.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Author: zjq
 * @Date: create in 2024/1/3 18:18
 */
@Service("emailService")
public class EmailServiceImpl implements EmailService {


    @Autowired
    private JavaMailSender javamailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${img-path}")
    private String img;

    @Value("${pdf-path}")
    private String pdfPath;

    public static final String SUBJECT = "综合素质报告单";

    @Override
    public void sendMail(String email, String code) throws MessagingException {

        MimeMessage mimeMailMessage = this.javamailSender.createMimeMessage();

        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMailMessage, true, "utf-8");
        messageHelper.setFrom(from);// 发送人
        messageHelper.setTo(email);// 接收人
        messageHelper.setSubject(SUBJECT);// 邮件主题
        messageHelper.addAttachment("mytest.pdf", new File(img));//附件


//        messageHelper.setText("<html><body><h1>这是一封带有图片的HTML邮件</h1><p>以下是图片内容：</p><img src=\"cid:logo\"></body></html>", true);// 邮件文本
//        messageHelper.addInline("logo", new File(img + File.separator + "2.png"));//行里插入图片

        javamailSender.send(mimeMailMessage);
        System.out.println("发送邮件");
    }
}
